/*
 * BookmarksView.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Felix(felix@ulise.cs.pub.ro)
 * 
 * Obs: N/A
 */

package ide;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

/**
 * shows the bookmarks in the current project
 * 
 * Verisim IDE 1.0
 * @version 1.0
 * @author Lucian Felix Ghita
 */
@SuppressWarnings("serial")
public class BookmarksView extends JInternalFrame {

	JList list;
	private Project project;

	public BookmarksView(Project project) {
		super("Bookmarks", true, true, true, true);
		vide.spl.showStatus("Creating bookarks manager..");

		this.project = project;
		project.bv = this;

		DefaultListModel listModel = new DefaultListModel();
		list = new JList(listModel);
		MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) itemDClicked(list.locationToIndex(e.getPoint()));
			}};
			list.addMouseListener(mouseListener);

			JScrollPane listScroller = new JScrollPane(list);
			listScroller.setBackground(list.getBackground());

			JPanel p = new JPanel();    //panel pt. lista.
			p.setBorder(new EmptyBorder(0, 0, 0, 0));
			p.setLayout(new BorderLayout());
			p.add(listScroller, BorderLayout.CENTER);

			JPanel bp = new JPanel();   //panel pt butoane
			bp.setBorder(new EmptyBorder(5, 3, 3, 5));
			bp.setLayout(new BorderLayout());

			JButton addButton = new JButton("Add");
			addButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					addBookmark();
				}});

			JButton removeButton = new JButton("Del");
			removeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					removeBookmark();
				}});

			bp.add(removeButton, BorderLayout.WEST);
			bp.add(addButton, BorderLayout.EAST);

			p.add(bp, BorderLayout.SOUTH);
			getContentPane().add(p);
			setSize(325, 200);
			setLocation(175, 75);
	}


	public void addBookmark() {
		String name = JOptionPane.showInputDialog("Bookmark name:");
		project.addBookmark(name);
		((DefaultListModel)list.getModel()).addElement("bookmark:" + name);
		//lista nu suporta direct inserari shi stergeri!
	}

	public void removeBookmark() {
		String s;
		if ((s = (String) list.getSelectedValue()) != null) {
			project.removeBookmark(s);  //poa' sa fie shi null
			((DefaultListModel)list.getModel()).removeElement(s);
		}
	}

	/**
	 * deschide o fereastra de editare (daca nu e deja deschisa)
	 * pt. fisierul din bookmark,
	 * o aduce in fata
	 * shi sare la linia respectiva.*/
	public void itemDClicked(int index) {
		project.jumpToBookmark((String)(((DefaultListModel)list.getModel()).elementAt(index)));
	}
}
